from django.db import models
import datetime


class Project(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)
    start_time = models.DateField(default=datetime.date.today)
    leader = models.ForeignKey('user.User', to_field='id', on_delete=models.PROTECT, db_column='leader')
    current_version = models.CharField(max_length=200, default='')
    detail = models.TextField(default='')

    class Meta:
        db_table = 'project'
        ordering = ['id']


class ProjectRole(models.Model):
    id = models.BigAutoField(primary_key=True)
    pid = models.ForeignKey('Project', to_field='id', on_delete=models.CASCADE, db_column='pid')
    name = models.CharField(max_length=200)

    class Meta:
        db_table = 'project_role'
        ordering = ['id']


class ProjectPermission(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.CharField(max_length=200)

    class Meta:
        db_table = 'project_permission'
        ordering = ['id']


class Participate(models.Model):
    uid = models.ForeignKey('user.User', to_field='id', on_delete=models.CASCADE, db_column='uid')
    rid = models.ForeignKey('ProjectRole', to_field='id', on_delete=models.PROTECT, db_column='rid')
    annotation = models.CharField(max_length=200, default='')

    class Meta:
        db_table = 'participate'
        constraints = [
            models.UniqueConstraint(fields=['uid', 'rid'], name='participate_id')
        ]


class ProjectEmpower(models.Model):
    rid = models.ForeignKey('ProjectRole', to_field='id', on_delete=models.CASCADE, db_column='rid')
    pid = models.ForeignKey('ProjectPermission', to_field='id', on_delete=models.CASCADE, db_column='pid')

    class Meta:
        db_table = 'project_empower'
        constraints = [
            models.UniqueConstraint(fields=['rid', 'pid'], name='project_empower_id')
        ]


class ProjectCodeRepository(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)
    href = models.CharField(max_length=200)
    pid = models.ForeignKey('Project', to_field='id', on_delete=models.CASCADE, db_column='pid')

    class Meta:
        db_table = 'project_code_repository'
        ordering = ['id']


class ProjectDocument(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)
    href = models.CharField(max_length=200)
    pid = models.ForeignKey('Project', to_field='id', on_delete=models.CASCADE, db_column='pid')

    class Meta:
        db_table = 'project_document'
        ordering = ['id']
